package com.fresh.service.impl;


import com.fresh.entity.User;
import com.fresh.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.fresh.qo.UserQO;
import com.fresh.service.VerifyService;

import java.util.HashMap;
import java.util.Map;

@Service
public class VerifyServiceImpl implements VerifyService {

    @Value("${chinadata.verify.host}")
    private String host;
    @Value("${chinadata.verify.path}")
    private String path;
    @Value("${chinadata.verify.appcode}")
    private String appCode;


    @Override
    public String verifyIdCard(UserQO userQO) {
        String name = userQO.getName();
        String idCard = userQO.getIdCard();
        String method = "POST";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appCode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<>();
        Map<String, String> bodys = new HashMap<>();
        bodys.put("name", name);
        bodys.put("idcard", idCard);
        String res = null;
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            //获取response的body
             res = EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public void updateIdCard(User user) {
        //TODO 写入数据库
    }
}
